package fr.cdj.thebox.client.menu;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.inject.Inject;

import fr.cdj.thebox.client.events.DeleteFilesRequestEvent;
import fr.cdj.thebox.client.events.DownloadFilesRequestEvent;
import fr.cdj.thebox.client.events.LogoutEvent;
import fr.cdj.thebox.client.events.SearchFilesRequestEvent;
import fr.cdj.thebox.client.events.SortFilesRequestEvent;
import fr.cdj.thebox.client.menu.ui.MenuDisplay;
import fr.cdj.thebox.shared.FileMetaData;

public class MenuPresenter extends WidgetPresenter<MenuDisplay> {

	@Inject
	public MenuPresenter(MenuDisplay display, EventBus eventBus) {
		super(display, eventBus);
	}

	@Override
	protected void onBind() {
		registerHandler(display.getLogoutClickHandlers().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new LogoutEvent());
			}
		}));
		registerHandler(display.getDeleteClickHandlers().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new DeleteFilesRequestEvent());
			}
		}));
		registerHandler(display.getDownloadClickHandlers().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new DownloadFilesRequestEvent());
			}
		}));
		registerHandler(display.getSortClickHandlers().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				FileMetaData.setCompareBy(display.getSortValue());
				eventBus.fireEvent(new SortFilesRequestEvent());
			}
		}));
		registerHandler(display.getSearchValueChangedHandlers().addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				eventBus.fireEvent(new SearchFilesRequestEvent(event.getValue()));
			}
		}));
	}

	@Override
	protected void onUnbind() {
	}

	@Override
	protected void onRevealDisplay() {
	}

}
